from math import sqrt
import commands
import sys

#MAIN

# create file names
input_file = sys.argv[1]
file_mesh = input_file + '.mesh'
file_out  = input_file + '.out'
file_vtk  = input_file + '-0.vtk'

# remove old vtk file
command_remove_old_vtk = 'rm ' + input_file + '-*.vtk'
commands.getoutput(command_remove_old_vtk)


# open mesh file and grab mesh dimension, number of nodes, 
# number of elements and element type
log_mesh  = open (file_mesh,  'r')
lin = log_mesh.readline ()
while lin != '<Nodes>\n': 
    lin = log_mesh.readline ()
lin  = log_mesh.readline ()
lst  = lin.split ()
dim = len(lst) - 1;
while lst[0].isdigit ():
    nb_nodes = int(lst[0]) + 1 
    lin  = log_mesh.readline ()
    lst  = lin.split ()
while lin != '<Elements>\n': 
    lin = log_mesh.readline ()
lin  = log_mesh.readline ()
lst  = lin.split ()
elem_conn = len(lst) - 1;
while lst[0].isdigit ():
    nb_ele = int(lst[0]) + 1 
    lin  = log_mesh.readline ()
    lst  = lin.split ()
log_mesh.close ()

# open files
log_mesh  = open (file_mesh,  'r')
log_out = open (file_out, 'r')
log_vtk = open (file_vtk, 'a')

# print the nodes
log_vtk.write("# vtk DataFile Version 3.0 \nJive Export \nASCII \nDATASET UNSTRUCTURED_GRID \nPOINTS ")
log_vtk.write(str(nb_nodes))
log_vtk.write(" double\n")
while lin != '<Nodes>\n': 
    lin = log_mesh.readline ()
for i in range (0, nb_nodes):
    lin = log_mesh.readline ()
    lst = lin.split ()
    for j in range (0, dim):
      log_vtk.write(str(lst[j+1]))
      log_vtk.write("\t")
    if dim ==2:
      log_vtk.write("0.")
    log_vtk.write("\n")

# print element connectivity and element type
log_vtk.write("CELLS ")
log_vtk.write(str(nb_ele))
log_vtk.write(" ")
log_vtk.write(str(nb_ele*(elem_conn+1)))
log_vtk.write("\n")
if elem_conn==3  and dim==2 :
    elem_code = 5
if elem_conn==4  and dim==2 :
    elem_code = 9
if elem_conn==4  and dim==3 :
    elem_code = 10
if elem_conn==6  and dim==2 :
    elem_code = 22
if elem_conn==8  and dim==2 :
    elem_code = 23
if elem_conn==10 and dim==3 :
    elem_code = 24
if elem_conn==8  and dim==3 :
    elem_code = 12
while lin != '<Elements>\n': 
    lin = log_mesh.readline ()
for i in range (0, nb_ele):
    lin = log_mesh.readline ()
    lst = lin.split ()
    log_vtk.write(str(elem_conn))
    log_vtk.write("\t")
    for j in range (0, elem_conn):
        log_vtk.write(str(lst[j+1]))
        log_vtk.write("\t")
    log_vtk.write("\n")
log_vtk.write("CELL_TYPES ")
log_vtk.write(str(nb_ele))
log_vtk.write("\n")
for i in range (0, nb_ele):
    log_vtk.write(str(elem_code))
    log_vtk.write("\n")
log_mesh.close ()
log_vtk.close ()

# count the number of increments and tags in the output file
lin  = log_out.readline ()
lin  = log_out.readline ()
lst  = lin.split ()
tag = [1]
tag[0] = lin[17:len(lin)-16]
nb_incr = 1
while len(lst) != 0:
  for i in range (0, nb_nodes+7):
      lin = log_out.readline ()
  lst = lin.split ()
  tag_tmp  = lin[17:len(lin)-16]
  if tag_tmp == tag[0]:
      nb_incr = nb_incr + 1
  elif tag_tmp != tag[0] and nb_incr == 1:
      tag.append(tag_tmp)
nb_tag = len(tag)
log_out.close ()

# Create the according number of vtk files and remove all semicolon in vtk
command_remove_semicolon = 'sed -e \'s/;/ /g\' ' + file_vtk + ' > tttt && mv tttt ' + file_vtk 
commands.getoutput(command_remove_semicolon)
for i in range (1, nb_incr):
  command_duplicate_vtk_file = 'cp ' + file_vtk + ' ' +input_file + '-' + str(i) + '.vtk'
  commands.getoutput(command_duplicate_vtk_file)

# print the result tags
log_out = open (file_out, 'r')
for i in range (0, 4):
  lin = log_out.readline ()
for incr in range (0,nb_incr):
  file_vtk  = input_file + '-' + str(incr) + '.vtk' 
  log_vtk = open (file_vtk, 'a')
  log_vtk.write("POINT_DATA ")
  log_vtk.write(str(nb_nodes))
  log_vtk.write("\n")
  for i in range (0, nb_tag):
    if dim == 3 and (tag[i] == 'stress' or tag[i] == 'strain'):
      log_vtk.write("TENSORS ")
      log_vtk.write(tag[i])
      log_vtk.write(" double")
      log_vtk.write("\n")
      tag_size = 9
    else:
      log_vtk.write("VECTORS ")
      log_vtk.write(tag[i])
      log_vtk.write(" double")
      log_vtk.write("\n")
      tag_size = 3
    for i in range (0, nb_nodes):
	lin = log_out.readline ()
	lst = lin.split ()
	for j in range (0, dim):
	    log_vtk.write(str(lst[j+1]))
	    log_vtk.write("\t")
	for j in range (dim+1, tag_size+1):
	  log_vtk.write("0.")
	  log_vtk.write("\t")
	log_vtk.write("\n")
    for i in range (0,7):
	lin = log_out.readline ()
  log_vtk.close ()
log_out.close ()


